package com.flavors.ifly.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.flavors.ifly.hencoderplus.MyButton;
import com.plat.sdk.R;

/**
 * @author ydong
 * Android View 事件分发机制 源码解析 （上）
 * https://blog.csdn.net/lmj623565791/article/details/38960443
 * 好了，跟View事件相关一般就这三个地方了，一个onTouchEvent，一个dispatchTouchEvent，一个setOnTouchListener;
 * 好了，可以看到，不管是DOWN，MOVE，UP都会按照下面的顺序执行：
 * 1、dispatchTouchEvent
 * 2、 setOnTouchListener的onTouch
 * 3、onTouchEvent
 * <<<总结>>>
 * 整个View的事件转发流程是：
 * View.dispatchTouchEvent->View.setOnTouchListener->View.onTouchEvent
 * 在dispatchTouchEvent中会进行OnTouchListener的判断，如果OnTouchListener不为null且返回true，则表示事件被消费，onTouchEvent不会被执行；否则执行onTouchEvent。
 */
public class MyButtonActivity extends AppCompatActivity {


    private static final String TAG = "MyButton";
    private MyButton mButtonOne;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_my_button);
        initView();
        Log.d("ydong", "onCreate");
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        mButtonOne = findViewById(R.id.my_botton_one);
        mButtonOne.setOnTouchListener((v, event) -> {
            int action = event.getAction();
            float x = v.getX();
            float y = v.getY();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    Log.e(TAG, "onTouch ACTION_DOWN");
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.e(TAG, "onTouch ACTION_MOVE");
                    break;
                case MotionEvent.ACTION_UP:
                    Log.e(TAG, "onTouch ACTION_UP");
                    break;
                default:
                    break;
            }
            return false;//这块如果返回true则因为
        });
        mButtonOne.setOnClickListener(v -> {
            Toast.makeText(MyButtonActivity.this, "OnClick", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "setOnClickListener onClick");
        });
        mButtonOne.setOnLongClickListener(v -> {
            Toast.makeText(MyButtonActivity.this, "onLongClick", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "setOnLongClickListener onLongClick");
            return false;
        });
    }



    @Override
    protected void onResume() {
        super.onResume();
        Log.d("ydong", "onResume");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d("ydong", "onNewIntent");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("ydong", "onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("ydong", "onStop");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("ydong", "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("ydong", "onDestroy");
    }


}
